package com.example.his.api.front.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.his.api.db.dao.GoodsDao;
import com.example.his.api.db.dao.OrderDao;
import com.example.his.api.db.pojo.OrderEntity;
import com.example.his.api.exception.HisException;
import com.example.his.api.front.service.GoodsService;
import com.example.his.api.front.service.OrderService;
import com.example.his.api.front.service.PayService;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huixh
 */
@Slf4j
@Service("FrontOrderServiceImpl")
public class OrderServiceImpl implements OrderService {

    @Resource
    private PayService payService;

    @Resource
    private OrderDao orderDao;

    @Resource
    private GoodsDao goodsDao;

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public Map<String, Object> createPayment(Map<String, Object> map) {
        Integer goodsId = MapUtil.getInt(map, "goodsId");
        Integer number = MapUtil.getInt(map, "number");
        Integer customerId = MapUtil.getInt(map, "customerId");

        boolean isIllegal = orderDao.searchIllegalCountInDay(customerId);
        if (!isIllegal) {
            // 不能购买
            return null;
        }
        // 查找商品详情信息
        HashMap<String, Object> stringObjectHashMap = goodsDao.searchSnapshotNeededById(goodsId);
        String goodsCode = MapUtil.getStr(stringObjectHashMap, "code");
        String goodsTitle = MapUtil.getStr(stringObjectHashMap, "title");
        String goodsDescription = MapUtil.getStr(stringObjectHashMap, "description");
        String goodsImage = MapUtil.getStr(stringObjectHashMap, "image");
        BigDecimal goodsInitialPrice = new BigDecimal(MapUtil.getStr(stringObjectHashMap, "initialPrice"));
        BigDecimal goodsCurrentPrice = new BigDecimal(MapUtil.getStr(stringObjectHashMap, "currentPrice"));
        String goodsRuleName = MapUtil.getStr(stringObjectHashMap, "ruleName");
        String goodsRule = MapUtil.getStr(stringObjectHashMap, "rule");
        String goodsType = MapUtil.getStr(stringObjectHashMap, "type");
        String goodsMd5 = MapUtil.getStr(stringObjectHashMap, "md5");

        String temp = MapUtil.getStr(stringObjectHashMap, "checkup_1");
        List<Map> goodsCheckup_1 = temp != null ? JSONUtil.parseArray(temp).toList(Map.class) :null;
        temp = MapUtil.getStr(stringObjectHashMap, "checkup_2");
        List<Map> goodsCheckup_2 = temp != null ? JSONUtil.parseArray(temp).toList(Map.class) :null;
        temp = MapUtil.getStr(stringObjectHashMap, "checkup_3");
        List<Map> goodsCheckup_3 = temp != null ? JSONUtil.parseArray(temp).toList(Map.class) :null;
        temp = MapUtil.getStr(stringObjectHashMap, "checkup_4");
        List<Map> goodsCheckup_4 = temp != null ? JSONUtil.parseArray(temp).toList(Map.class) :null;

        temp = MapUtil.getStr(stringObjectHashMap, "checkup");
        List<Map> goodsCheckup = temp != null ? JSONUtil.parseArray(temp).toList(Map.class) :null;
        temp = MapUtil.getStr(stringObjectHashMap, "tag");
        List<String> goodsTag = temp != null ? JSONUtil.parseArray(temp).toList(String.class) :null;

        // 根据规则 计算 折扣
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<>();
        context.put("number", number);
        context.put("price",goodsCurrentPrice.toString());
        String amount = null;
        if (goodsRule != null) {
            try {
                amount = runner.execute(goodsRule, context, null, true, false).toString();
            } catch (Exception e) {
                throw new HisException("订单规则计算失败",e);
            }
        } else {
            amount = goodsCurrentPrice.multiply(BigDecimal.valueOf(number)).toString();
        }

        // 计算订单金额

        // 创建支付宝订单

        // 创建支付单缓存 设置缓存过期时间

         // 如果 不存在订单 该商品快照 创建快照

        // 保存订单记录

        // 更新 商品笑销量

        // 付款二维码图片转换成 base64 字符串 返回给前端

        return null;
    }
}
